package com.plan.dao.service.card.impl;

import com.plan.base.constant.BaseCode;
import com.plan.base.exception.BizException;
import com.plan.controller.card.request.ExchangeLogPageListReq;
import com.plan.controller.card.response.ExchangeLogListRes;
import com.plan.dao.mapper.card.CardMapper;
import com.plan.dao.mapper.card.ExchangeLogMapper;
import com.plan.dao.mapper.card.PointLogMapper;
import com.plan.dao.service.card.ExchangeDaoService;
import com.plan.entity.card.Card;
import com.plan.entity.card.ExchangeLog;
import com.plan.entity.card.PointLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExchangeDaoServiceImpl implements ExchangeDaoService {
    @Resource
    private PointLogMapper pointLogMapper;
    @Resource
    private ExchangeLogMapper exchangeLogMapper;
    @Resource
    private CardMapper cardMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long exchange(Card card, ExchangeLog exchangeLog, PointLog pointLog) {
        exchangeLogMapper.insert(exchangeLog);
        pointLogMapper.insert(pointLog);
        int num = cardMapper.update(card);
        if (num != 1) {
            throw new BizException(BaseCode.DATA_BE_MODIFIED);
        }
        return exchangeLog.getId();
    }

    @Override
    public List<ExchangeLogListRes> getExchangeLogList(ExchangeLogPageListReq req) {
        return exchangeLogMapper.getExchangeLogList(req);
    }
}
